package com.kh.pajx.sop.view.action.bal;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.kh.pajx.sop.base.DatabaseContextHolder;
import com.kh.pajx.sop.base.ModelDrivenBaseAction;
import com.kh.pajx.sop.base.SOPConfig;
import com.kh.pajx.sop.domain.SysInsideUser;
import com.kh.pajx.sop.domain.bal.BalBuMgrDrive;
import com.kh.pajx.sop.domain.bal.BalCashDrive;
import com.kh.pajx.sop.domain.bal.BalCustMgrDrive;
import com.kh.pajx.sop.domain.bal.BalMainBill;
import com.kh.pajx.sop.domain.bal.BalTelfeeDrive;
import com.kh.pajx.sop.service.bal.IBalBuMgrDriveService;
import com.kh.pajx.sop.service.bal.IBalCashDriveService;
import com.kh.pajx.sop.service.bal.IBalCustMgrDriveService;
import com.kh.pajx.sop.service.bal.IBalMainBillService;
import com.kh.pajx.sop.service.bal.IBalTelfeeDriveService;
import com.kh.pajx.sop.util.common.DwzAjaxResult;
import com.kh.pajx.sop.util.common.FormatDate;
import com.kh.pajx.sop.util.common.MathExtend;
import com.kh.pajx.sop.util.common.ValidatorUtils;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class BalAction extends ModelDrivenBaseAction<BalMainBill>{
	
	@Resource
	private IBalBuMgrDriveService balBuMgrDriveService;
	
	@Resource 
	private IBalCustMgrDriveService balCustMgrDriveService;
	
	@Resource 
	private IBalCashDriveService balCashDriveService;
	
	@Resource 
	private IBalTelfeeDriveService balTelfeeDriveService;
	
	@Resource
	private IBalMainBillService balMainBillService;

	private static final long serialVersionUID = -3087898289117036548L;
	
	public String appMain(){
		try {
			
			String roleCode = getCurrentUser().getUserRole().getUsrCode();
			
			if(!(roleCode.equals(SOPConfig.CODE_USER_ROLE_TOP_MANAGER) || (roleCode.equals(SOPConfig.CODE_USER_ROLE_EXTEND) && getCurrentUser().getInsideUser().getManagerFlag().equals("1")))){
				return "noPrivilegeError";
			}
			
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			// 获取新增行业经理季度结算
			List<BalBuMgrDrive> balBuMgrList = balBuMgrDriveService.findNewList();
			//balBuMgrList.clear();
			ActionContext.getContext().put("balBuMgrList", balBuMgrList);
			
			// 获取新增移动客户经理结算
			List<BalCustMgrDrive> BalCustMgrList = balCustMgrDriveService.findNewList();
			//BalCustMgrList.clear();
			ActionContext.getContext().put("BalCustMgrList", BalCustMgrList);
			
			// 获取现金激励学校结算
			List<BalCashDrive> balCashDriveList = balCashDriveService.findNewList();
			//balCashDriveList.clear();
			ActionContext.getContext().put("balCashDriveList", balCashDriveList);
			
			// 获取话费激励学校结算
			List<BalTelfeeDrive> balTelfeeDriveList = balTelfeeDriveService.findNewList();
			//balTelfeeDriveList.clear();
			ActionContext.getContext().put("balTelfeeDriveList", balTelfeeDriveList);
			
			String firstTab = "";
			String firstRel = "";
			if(balTelfeeDriveList!=null && balTelfeeDriveList.size()>0){
				firstTab = "telfeeNewList";
				firstRel = "bal_telfee_new_list";
			}else if(balCashDriveList!=null && balCashDriveList.size()>0){
				firstTab = "cashNewList";
				firstRel = "bal_cash_new_list";
			}else if(BalCustMgrList!=null && BalCustMgrList.size()>0){
				firstTab = "custMgrNewList";
				firstRel = "bal_custmgr_new_list";
			}else if(balBuMgrList!=null && balBuMgrList.size()>0){
				firstTab = "buMgrNewList";
				firstRel = "bal_bumgr_new_list";
			}
			ActionContext.getContext().put("firstTab", firstTab);
			ActionContext.getContext().put("firstRel", firstRel);
			
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "appMain";
	}
	
	
	//********************************申请页面**************************************//
	
	/** 显示可以申请的移动客户经理结算明细列表 */
	public String custMgrNewList(){
		try {
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balCustMgrDriveService.showNewList(getCurrentUser().getDepartment().getDeptCode(), getPageNum(), getNumPerPage());
			ActionContext.getContext().put("currentDate", FormatDate.getDefaultDate());
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "custMgrNewList";
	}
	
	/** 显示可以申请的行业经理结算明细列表 */
	public String buMgrNewList(){
		try {
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balBuMgrDriveService.showNewList(getCurrentUser().getDepartment().getDeptCode(),getPageNum(), getNumPerPage());
			ActionContext.getContext().put("currentDate", FormatDate.getDefaultDate());
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "buMgrNewList";
	}
	
	/** 显示可以申请的现金激励学校结算明细列表 */
	public String cashNewList(){
		try {
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balCashDriveService.showNewList(getCurrentUser().getDepartment().getDeptCode(),getPageNum(), getNumPerPage());
			ActionContext.getContext().put("currentDate", FormatDate.getDefaultDate());
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "cashNewList";
	}
	
	/** 显示可以申请的话费激励学校结算明细列表 */
	public String telfeeNewList(){
		try {
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balTelfeeDriveService.showNewList(getCurrentUser().getDepartment().getDeptCode(),getPageNum(), getNumPerPage());
			ActionContext.getContext().put("currentDate", FormatDate.getDefaultDate());
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "telfeeNewList";
	}
	
	/** 我的申请 主页面 */
	public String myAppMain(){
		String roleCode = getCurrentUser().getUserRole().getUsrCode();
		if(!(roleCode.equals(SOPConfig.CODE_USER_ROLE_TOP_MANAGER) || (roleCode.equals(SOPConfig.CODE_USER_ROLE_EXTEND) && getCurrentUser().getInsideUser().getManagerFlag().equals("1")))){
			return "noPrivilegeError";
		}
		return "myAppMain";
	}
	
	/** 我的申请 列表页面 */
	public String myAppList(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, true)){
				throw new Exception("单据编号传入错误，应为8位纯数字组成！");
			}
			DatabaseContextHolder.setCustomerType("dataSourceOne");
			List<SysInsideUser> saleList = sysInsideUserService.findAll();
			ActionContext.getContext().put("saleList", saleList);
			DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balMainBillService.findMyApp(model.getBillCode(),model.getBalType(),model.getBillState(),getCurrentUser().getUserId(), getPageNum(), getNumPerPage());
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "myAppList";
	}
	
	
	/** 结算审批 主页面 */
	public String checkMain(){
		return "checkMain";
	}
	
	/** 结算审批 列表页面 */
	public String checkList(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, true)){
				throw new Exception("单据编号传入错误，应为8位纯数字组成！");
			}
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBalType(), 1, true)){
				throw new Exception("结算类型选择错误！");
			}
			DatabaseContextHolder.setCustomerType("dataSourceOne");
			List<SysInsideUser> saleList = sysInsideUserService.findAll();
			ActionContext.getContext().put("saleList", saleList);
			DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balMainBillService.findCheck(model.getBillCode(),model.getBalType(), getPageNum(), getNumPerPage());
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "checkList";
	}
	
	/** 我的审批 主页面 */
	public String myCheckMain(){
		return "myCheckMain";
	}
	
	/** 我的审批列表页面 */
	public String myCheckList(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, true)){
				throw new Exception("单据编号传入错误，应为8位纯数字组成！");
			}
			DatabaseContextHolder.setCustomerType("dataSourceOne");
			List<SysInsideUser> saleList = sysInsideUserService.findAll();
			ActionContext.getContext().put("saleList", saleList);
			DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balMainBillService.findMyCheck(model.getBillCode(),model.getBalType(),getCurrentUser().getUserId(), getPageNum(), getNumPerPage());
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "myCheckList";
	} 
	
	

	
	public String myMakMain(){
		return "myMakMain";
	}
	
	public String myMakList(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, true)){
				throw new Exception("单据编号传入错误，应为8位纯数字组成！");
			}
			DatabaseContextHolder.setCustomerType("dataSourceOne");
			List<SysInsideUser> saleList = sysInsideUserService.findAll();
			ActionContext.getContext().put("saleList", saleList);
			DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balMainBillService.findMyMak(model.getBillCode(),model.getBalType(),getCurrentUser().getUserId(), getPageNum(), getNumPerPage());
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "myMakList";
	}
	
	//********************************审批页面**************************************//
	
	/** 行业经理结算申请单据审批 */
	public String showBuMgrCheck(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, false)){
				throw new Exception("无法解析结算单据编号！");
			}
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			
			BalMainBill bill = balMainBillService.getByCode(model.getBillCode());
			if(null == bill){
				throw new Exception("该单据不存在或已被撤销！");
			}
			if(bill.getBillState()!=1){
				throw new Exception("该单据已不符合审批条件！");
			}
			
			List<BalBuMgrDrive> balList = balBuMgrDriveService.findList(model.getBillCode(),1);
			double totalFee = 0.0;
			for (BalBuMgrDrive buMgr : balList) {
				if(buMgr.getBalFee()!=null){
					totalFee = MathExtend.add(totalFee, buMgr.getBalFee());
				}
			}
			ActionContext.getContext().getValueStack().push(bill);
			ActionContext.getContext().put("balList", balList);
			ActionContext.getContext().put("totalFee", totalFee);
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "showBuMgrCheck";
	}
	
	/** 客户经理结算申请单据审批 */
	public String showCustMgrCheck(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, false)){
				throw new Exception("无法解析结算单据编号！");
			}
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			
			BalMainBill bill = balMainBillService.getByCode(model.getBillCode());
			if(null == bill){
				throw new Exception("该单据不存在或已被撤销！");
			}
			if(bill.getBillState()!=1){
				throw new Exception("该单据已不符合审批条件！");
			}
			
			List<BalCustMgrDrive> balList = balCustMgrDriveService.findList(model.getBillCode(),1);
			double totalFee = 0.0;
			for (BalCustMgrDrive custMgr : balList) {
				if(custMgr.getBalFee()!=null){
					totalFee = MathExtend.add(totalFee, custMgr.getBalFee());
				}
			}
			ActionContext.getContext().getValueStack().push(bill);
			ActionContext.getContext().put("balList", balList);
			ActionContext.getContext().put("totalFee", totalFee);
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "showCustMgrCheck";
	}
	
	/** 学校现金结算单据审批 */
	public String showCashCheck(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, false)){
				throw new Exception("无法解析结算单据编号！");
			}
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			
			BalMainBill bill = balMainBillService.getByCode(model.getBillCode());
			if(null == bill){
				throw new Exception("该单据不存在或已被撤销！");
			}
			if(bill.getBillState()!=1){
				throw new Exception("该单据已不符合审批条件！");
			}
			
			List<BalCashDrive> balList = balCashDriveService.findList(model.getBillCode(),1);
			double totalFee = 0.0;
			for (BalCashDrive cash : balList) {
				if(cash.getBalFee()!=null){
					totalFee = MathExtend.add(totalFee, cash.getBalFee());
				}
			}
			ActionContext.getContext().getValueStack().push(bill);
			ActionContext.getContext().put("balList", balList);
			ActionContext.getContext().put("totalFee", totalFee);
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		
		return "showCashCheck";
	}
	
	/** 学校话费结算单据审批 */
	public String showTelfeeCheck(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, false)){
				throw new Exception("无法解析结算单据编号！");
			}
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			
			BalMainBill bill = balMainBillService.getByCode(model.getBillCode());
			if(null == bill){
				throw new Exception("该单据不存在或已被撤销！");
			}
			if(bill.getBillState()!=1){
				throw new Exception("该单据已不符合审批条件！");
			}
			
			List<BalTelfeeDrive> balList = balTelfeeDriveService.findList(model.getBillCode(),1);
			double totalFee = 0.0;
			for (BalTelfeeDrive telfee : balList) {
				if(telfee.getBalFee()!=null){
					totalFee = MathExtend.add(totalFee, telfee.getBalFee());
				}
			}
			ActionContext.getContext().getValueStack().push(bill);
			ActionContext.getContext().put("balList", balList);
			ActionContext.getContext().put("totalFee", totalFee);
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "showTelfeeCheck";
	}
	
	/** 行业经理结算审批历史 */
	public String showBuMgrCheckHistory(){
		try {
			if(!ValidatorUtils.validateUUID(model.getUuid(), false)){
				throw new Exception("传入参数不符合规范！");
			}
			
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			
			BalBuMgrDrive buMgrDrive = balBuMgrDriveService.getById(model.getUuid());
			ActionContext.getContext().put("buMgrDrive", buMgrDrive);
			balBuMgrDriveService.findBuMgrHisByAreaCode(buMgrDrive.getAreaCode(), getPageNum(), getNumPerPage());
			
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "showBuMgrCheckHistory";
	}
	
	/** 客户经理结算审批历史 */
	public String showCustMgrCheckHistory(){
		try {
			if(!ValidatorUtils.validateUUID(model.getUuid(), false)){
				throw new Exception("传入参数不符合规范！");
			}
			
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			BalCustMgrDrive custMgr = balCustMgrDriveService.getById(model.getUuid());
			ActionContext.getContext().put("custMgr", custMgr);
			balCustMgrDriveService.findCustMgrHisBySclId(custMgr.getSclId(), getPageNum(), getNumPerPage());
			
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "showCustMgrCheckHistory";
	}
	
	/** 学校现金结算审批历史 */
	public String showCashCheckHistory(){
		try {
			if(!ValidatorUtils.validateUUID(model.getUuid(), false)){
				throw new Exception("传入参数不符合规范！");
			}
			
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			BalCashDrive cashDrive = balCashDriveService.getById(model.getUuid());
			ActionContext.getContext().put("cashDrive", cashDrive);
			balCashDriveService.findCashHisBySclId(cashDrive.getSclId(), getPageNum(), getNumPerPage());
			
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "showCashCheckHistory";
	}
	
	/** 学校话费结算审批历史 */
	public String showTelfeeCheckHistory(){
		try {
			if(!ValidatorUtils.validateUUID(model.getUuid(), false)){
				throw new Exception("传入参数不符合规范！");
			}
			
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			BalTelfeeDrive telfee = balTelfeeDriveService.getById(model.getUuid());
			ActionContext.getContext().put("telfee", telfee);
			balTelfeeDriveService.findTelfeeHisBySclId(telfee.getSclId(), getPageNum(), getNumPerPage());
			
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "showTelfeeCheckHistory";
	}
	

	
	
	
	
	
	
	//********************************显示单据详情**************************************//
	
	public String showBuMgrDetail(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, false)){
				throw new Exception("无法解析结算单据编号！");
			}
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balMainBillService.findBuMgrDetail(model.getBillCode(), getPageNum(), getNumPerPage());
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "showBuMgrDetail";
	}
	
	public String showCustMgrDetail(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, false)){
				throw new Exception("无法解析结算单据编号！");
			}
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balMainBillService.findCustMgrDetail(model.getBillCode(), getPageNum(), getNumPerPage());
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "showCustMgrDetail";
	}
	
	public String showCashDetail(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, false)){
				throw new Exception("无法解析结算单据编号！");
			}
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balMainBillService.findCashDetail(model.getBillCode(), getPageNum(), getNumPerPage());
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "showCashDetail";
	}
	
	public String showTelfeeDetail(){
		try {
			
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, false)){
				throw new Exception("无法解析结算单据编号！");
			}
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balMainBillService.findTelfeeDetail(model.getBillCode(), getPageNum(), getNumPerPage());
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "showTelfeeDetail";
	}
	
	
	
	//********************************提交申请业务处理**************************************//
	
	/** 提交话费激励学校结算申请 */
	public void appTelFee(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			String balIds = ServletActionContext.getRequest().getParameter("balIds");
			String totalFee = ServletActionContext.getRequest().getParameter("totalFee");
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			if(!ValidatorUtils.validateLangIds(balIds,false)){
				throw new Exception("输入参数不符合规范，无法解析操作！");
			}
			if(!ValidatorUtils.isDouble(totalFee)){
				throw new Exception("申请金额传入有误，无法解析操作！");
			}
			balMainBillService.appTelFee(balIds, totalFee, getCurrentUser().getUserId(),getCurrentUser().getDepartment().getDeptCode());
			dwz.setStatusCode("200");
			dwz.setMessage("已成功提交结算申请！");
			dwz.setRel("bal_telfee_new_list");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setMessage("申请提交失败！<br>错误信息："+e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	/** 提交现金激励学校结算申请 */
	public void appCash(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			String balIds = ServletActionContext.getRequest().getParameter("balIds");
			String totalFee = ServletActionContext.getRequest().getParameter("totalFee");
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			if(!ValidatorUtils.validateLangIds(balIds,false)){
				throw new Exception("输入参数不符合规范，无法解析操作！");
			}
			if(!ValidatorUtils.isDouble(totalFee)){
				throw new Exception("申请金额传入有误，无法解析操作！");
			}
			balMainBillService.appCash(balIds, totalFee, getCurrentUser().getUserId(),getCurrentUser().getDepartment().getDeptCode());
			dwz.setStatusCode("200");
			dwz.setMessage("已成功提交结算申请！");
			dwz.setRel("bal_cash_new_list");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setMessage("申请提交失败！<br>错误信息："+e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	/** 提交移动客户经理结算申请 */
	public void appCustMgr(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			String balIds = ServletActionContext.getRequest().getParameter("balIds");
			String totalFee = ServletActionContext.getRequest().getParameter("totalFee");
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			if(!ValidatorUtils.validateLangIds(balIds,false)){
				throw new Exception("输入参数不符合规范，无法解析操作！");
			}
			if(!ValidatorUtils.isDouble(totalFee)){
				throw new Exception("申请金额传入有误，无法解析操作！");
			}
			balMainBillService.appCustMgr(balIds, totalFee, getCurrentUser().getUserId(),getCurrentUser().getDepartment().getDeptCode());
			dwz.setStatusCode("200");
			dwz.setMessage("已成功提交结算申请！");
			dwz.setRel("bal_custmgr_new_list");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setMessage("申请提交失败！<br>错误信息："+e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	/** 提交行业经理季度结算申请 */
	public void appBuMgr(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			String balIds = ServletActionContext.getRequest().getParameter("balIds");
			String totalFee = ServletActionContext.getRequest().getParameter("totalFee");
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			if(!ValidatorUtils.validateLangIds(balIds,false)){
				throw new Exception("输入参数不符合规范，无法解析操作！");
			}
			if(!ValidatorUtils.isDouble(totalFee)){
				throw new Exception("申请金额传入有误，无法解析操作！");
			}
			balMainBillService.appBuMgr(balIds, totalFee, getCurrentUser().getUserId(),getCurrentUser().getDepartment().getDeptCode());
			dwz.setStatusCode("200");
			dwz.setMessage("已成功提交结算申请！");
			dwz.setRel("bal_bumgr_new_list");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setMessage("申请提交失败！<br>错误信息："+e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	/** 撤销申请单据 */
	public void cancelApp(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			if(!ValidatorUtils.validateUUID(model.getUuid(), false)){
				throw new Exception("无法解析传入单据编号！");
			}
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balMainBillService.cancelApp(model.getUuid());
			dwz.setStatusCode("200");
			dwz.setMessage("已成功撤销结算申请！");
			dwz.setRel("bal_myapp_list");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setMessage("撤销失败！<br>错误信息："+e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	/** 结算单据审批 */
	public void checkApp(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			String[] uuidArray = ServletActionContext.getRequest().getParameterValues("bal_check_uuid");
			String[] payFeeArray = ServletActionContext.getRequest().getParameterValues("bal_check_payFee");
			String[] remarkArray = ServletActionContext.getRequest().getParameterValues("bal_check_declare");
			
			if(!getCurrentUser().getUserRole().getUsrCode().equals(SOPConfig.CODE_USER_ROLE_TOP_MANAGER)){
				throw new Exception("您没有结算单据审批权限！");
			}
			
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balMainBillService.checkApp(model,getCurrentUser().getUserId(), uuidArray, payFeeArray, remarkArray);
			
			dwz.setStatusCode("200");
			dwz.setMessage("已成功审批结算申请！");
			dwz.setRel("bal_check_list");
			dwz.setCallbackType("closeCurrent");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setMessage("审批失败！<br>错误信息："+e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	/** 撤销审批单据 */
	public void cancelCheck(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			if(!ValidatorUtils.validateUUID(model.getUuid(), false)){
				throw new Exception("无法解析传入单据编号！");
			}
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balMainBillService.cancelCheck(model.getUuid());
			dwz.setStatusCode("200");
			dwz.setMessage("已成功撤销结算审批！");
			dwz.setRel("bal_mycheck_list");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setMessage("撤销失败！<br>错误信息："+e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	/** 重置打印状态 */
	public void resetPntFlag(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			if(!ValidatorUtils.validateUUID(model.getUuid(), false)){
				throw new Exception("无法解析传入单据编号！");
			}
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			if(!getCurrentUser().getUserRole().getUsrCode().equals(SOPConfig.CODE_USER_ROLE_TOP_MANAGER)){
				throw new Exception("您没有重置打印状态权限！");
			}
			
			BalMainBill mainBill = balMainBillService.getById(model.getUuid());
			if(null == mainBill){
				throw new Exception("无对应单据信息！");
			}
			
			if(mainBill.getBillState() != 2){
				throw new Exception("该单据不符合重置要求！");
			}
			
			if(mainBill.getPntFlag()==0){
				throw new Exception("该单据无需重置打印状态！");
			}
			
			mainBill.setPntFlag(0);
			balMainBillService.update(mainBill);
			dwz.setStatusCode("200");
			dwz.setMessage("已成功重置打印状态！");
			dwz.setRel("bal_mycheck_list");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setMessage("重置失败！<br>错误信息："+e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	/** 结算单据签批 */
	public void makApp(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			
			if(!ValidatorUtils.validateUUID(model.getUuid(), false)){
				throw new Exception("无法解析传入单据编号！");
			}
			if(!getCurrentUser().getUserRole().getUsrCode().equals(SOPConfig.CODE_USER_ROLE_TOP_MANAGER)){
				throw new Exception("您没有结算单据签批权限！");
			}
			
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			balMainBillService.makApp(model, getCurrentUser().getUserId());
			
			dwz.setStatusCode("200");
			dwz.setMessage("已成功签批结算申请！");
			dwz.setRel("bal_mak_list");
			dwz.setCallbackType("closeCurrent");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setMessage("签批失败！<br>错误信息："+e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	/** 打印申请单据 */
	public void checkPrint(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			if(!ValidatorUtils.validateUUID(model.getUuid(), false)){
				throw new Exception("无法解析传入单据编号！");
			}
			
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			BalMainBill bill = balMainBillService.getById(model.getUuid());
			if(null == bill){
				throw new Exception("该单据不存在或已被删除！");
			}
			
			if(bill.getBillState()!=2){
				throw new Exception("该单据不允许打印！"); 
			}
			
			if(bill.getPntFlag()!=0){
				throw new Exception("该单据不允许多次打印！");
			}
			dwz.setStatusCode("200");
			if(bill.getBalType()==6){
				dwz.setForwardUrl("bal_showPrintBuMgr.action?uuid="+bill.getUuid());
				dwz.setNavTabId("bal_showPrintBuMgr");
				dwz.setRel("打印单据-["+bill.getBillCode()+"]-[行业经理结算]");
			}else if(bill.getBalType()==5){
				dwz.setForwardUrl("bal_showPrintCustMgr.action?uuid="+bill.getUuid());
				dwz.setNavTabId("bal_showPrintCustMgr");
				dwz.setRel("打印单据-["+bill.getBillCode()+"]-[客户经理结算]");
			}else if(bill.getBalType()==4){
				dwz.setForwardUrl("bal_showPrintCash.action?uuid="+bill.getUuid());
				dwz.setNavTabId("bal_showPrintCash");
				dwz.setRel("打印单据-["+bill.getBillCode()+"]-[学校现金结算]");
			}else if(bill.getBalType()==3){
				dwz.setForwardUrl("bal_showPrintTelFee.action?uuid="+bill.getUuid());
				dwz.setNavTabId("bal_showPrintTelFee");
				dwz.setRel("打印单据-["+bill.getBillCode()+"]-[学校话费结算]");
			}
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setMessage("操作失败！<br>错误信息："+e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	public String showPrintBuMgr(){
		try {
			if(!ValidatorUtils.validateUUID(model.getUuid(), false)){
				throw new Exception("无法解析传入单据编号！");
			}
			
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			BalMainBill bill = balMainBillService.getById(model.getUuid());
			if(null == bill){
				throw new Exception("该单据不存在或已被删除！");
			}
			
			if(bill.getBillState()!=2){
				throw new Exception("该单据不允许打印！"); 
			}
			
			if(bill.getPntFlag()!=0){
				throw new Exception("该单据不允许多次打印！");
			}
			
			
			List<BalBuMgrDrive> buMgrList = balBuMgrDriveService.findList(bill.getBillCode(), 2);
			ActionContext.getContext().put("buMgrList", buMgrList);
			ActionContext.getContext().getValueStack().push(bill);
			DatabaseContextHolder.setCustomerType("dataSourceOne");
			ActionContext.getContext().put("appUserName", sysInsideUserService.getById(bill.getAppUserId()).getIsuName());
			
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", "页面加载错误！"+e.getMessage());
		}
		return "showPrintBuMgr";
	}
	
	public String showPrintCustMgr(){
		try {
			if(!ValidatorUtils.validateUUID(model.getUuid(), false)){
				throw new Exception("无法解析传入单据编号！");
			}
			
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			BalMainBill bill = balMainBillService.getById(model.getUuid());
			if(null == bill){
				throw new Exception("该单据不存在或已被删除！");
			}
			
			if(bill.getBillState()!=2){
				throw new Exception("该单据不允许打印！"); 
			}
			
			if(bill.getPntFlag()!=0){
				throw new Exception("该单据不允许多次打印！");
			}
			
			
			List<BalCustMgrDrive> custMgrList = balCustMgrDriveService.findList(bill.getBillCode(), 2);
			ActionContext.getContext().put("custMgrList", custMgrList);
			ActionContext.getContext().getValueStack().push(bill);
			DatabaseContextHolder.setCustomerType("dataSourceOne");
			ActionContext.getContext().put("appUserName", sysInsideUserService.getById(bill.getAppUserId()).getIsuName());
			
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", "页面加载错误！"+e.getMessage());
		}
		return "showPrintCustMgr";
	}
	
	public String showPrintCash(){
		try {
			if(!ValidatorUtils.validateUUID(model.getUuid(), false)){
				throw new Exception("无法解析传入单据编号！");
			}
			
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			BalMainBill bill = balMainBillService.getById(model.getUuid());
			if(null == bill){
				throw new Exception("该单据不存在或已被删除！");
			}
			
			if(bill.getBillState()!=2){
				throw new Exception("该单据不允许打印！"); 
			}
			
			if(bill.getPntFlag()!=0){
				throw new Exception("该单据不允许多次打印！");
			}
			
			
			List<BalCashDrive> cashList = balCashDriveService.findList(bill.getBillCode(), 2);
			ActionContext.getContext().put("cashList", cashList);
			ActionContext.getContext().getValueStack().push(bill);
			DatabaseContextHolder.setCustomerType("dataSourceOne");
			ActionContext.getContext().put("appUserName", sysInsideUserService.getById(bill.getAppUserId()).getIsuName());
			
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", "页面加载错误！"+e.getMessage());
		}
		return "showPrintCash";
	}
	
	public String showPrintTelFee(){
		
		try {
			if(!ValidatorUtils.validateUUID(model.getUuid(), false)){
				throw new Exception("无法解析传入单据编号！");
			}
			
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			BalMainBill bill = balMainBillService.getById(model.getUuid());
			if(null == bill){
				throw new Exception("该单据不存在或已被删除！");
			}
			
			if(bill.getBillState()!=2){
				throw new Exception("该单据不允许打印！"); 
			}
			
			if(bill.getPntFlag()!=0){
				throw new Exception("该单据不允许多次打印！");
			}
			
			
			List<BalTelfeeDrive> telfeeList = balTelfeeDriveService.findList(bill.getBillCode(), 2);
			ActionContext.getContext().put("telfeeList", telfeeList);
			ActionContext.getContext().getValueStack().push(bill);
			DatabaseContextHolder.setCustomerType("dataSourceOne");
			ActionContext.getContext().put("appUserName", sysInsideUserService.getById(bill.getAppUserId()).getIsuName());
			
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", "页面加载错误！"+e.getMessage());
		}
		
		return "showPrintTelfee";
	}
	
	public void print(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			if(!ValidatorUtils.validateUUID(model.getUuid(), false)){
				throw new Exception("无法解析传入单据编号！");
			}
			
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			BalMainBill bill = balMainBillService.getById(model.getUuid());
			if(null == bill){
				throw new Exception("该单据不存在或已被删除！");
			}
			
			if(bill.getBillState()!=2){
				throw new Exception("该单据不允许打印！"); 
			}
			
			if(bill.getPntFlag()!=0){
				throw new Exception("该单据不允许多次打印！");
			}
			
			bill.setPntFlag(1);
			balMainBillService.update(bill);
			
			dwz.setStatusCode("200");
			dwz.setMessage("已成功调去打印机，打印标识已更改！");
			dwz.setRel("bal_myapp_list");
			dwz.setCallbackType("closeCurrent");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", "未知错误！"+e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	//*****************************************************************************************************//
	//*****************************************************************************************************//
	//*****************************************************************************************************//
	//**************************************  结  算  审  批   ******************************************//
	//*****************************************************************************************************//
	//*****************************************************************************************************//
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//**************************************  结  算  签  批   ***************************************************//
	
	/** 结算签批 */
	public String makMain(){
		return "makMain";
	}
	
	/** 结算签批列表 */
	public String makList(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, true)){
				throw new Exception("单据编号传入错误，应为8位纯数字组成！");
			}
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBalType(), 1, true)){
				throw new Exception("结算类型选择错误！");
			}
			
			balMainBillService.findMak(model.getBillCode(),model.getBalType(), getPageNum(), getNumPerPage());
			
			DatabaseContextHolder.setCustomerType("dataSourceOne");
			List<SysInsideUser> saleList = sysInsideUserService.findAll();
			ActionContext.getContext().put("saleList", saleList);
			
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "makList";
	}
	
	/** 行业经理结算申请单据签批*/
	public String showBuMgrMak(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, false)){
				throw new Exception("无法解析结算单据编号！");
			}
			
			BalMainBill bill = balMainBillService.getByCode(model.getBillCode());
			if(null == bill){
				throw new Exception("该单据不存在或已被撤销！");
			}
			if(bill.getBillState()!=2){
				throw new Exception("该单据已不符合签批条件！");
			}
			
			List<BalBuMgrDrive> balList = balBuMgrDriveService.findList(model.getBillCode(),2);
			double totalFee = 0.0;
			for (BalBuMgrDrive buMgr : balList) {
				if(buMgr.getBalFee()!=null){
					totalFee = MathExtend.add(totalFee, buMgr.getBalFee());
				}
			}
			ActionContext.getContext().getValueStack().push(bill);
			ActionContext.getContext().put("balList", balList);
			ActionContext.getContext().put("totalFee", totalFee);
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "showBuMgrMak";
	}
	
	/** 客户经理结算申请单据签批 */
	public String showCustMgrMak(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, false)){
				throw new Exception("无法解析结算单据编号！");
			}
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			
			BalMainBill bill = balMainBillService.getByCode(model.getBillCode());
			if(null == bill){
				throw new Exception("该单据不存在或已被撤销！");
			}
			if(bill.getBillState()!=2){
				throw new Exception("该单据已不符合签批条件！");
			}
			
			List<BalCustMgrDrive> balList = balCustMgrDriveService.findList(model.getBillCode(),2);
			double totalFee = 0.0;
			for (BalCustMgrDrive custMgr : balList) {
				if(custMgr.getBalFee()!=null){
					totalFee = MathExtend.add(totalFee, custMgr.getBalFee());
				}
			}
			ActionContext.getContext().getValueStack().push(bill);
			ActionContext.getContext().put("balList", balList);
			ActionContext.getContext().put("totalFee", totalFee);
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "showCustMgrMak";
	}
	
	/** 学校现金结算单据签批 */
	public String showCashMak(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, false)){
				throw new Exception("无法解析结算单据编号！");
			}
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			
			BalMainBill bill = balMainBillService.getByCode(model.getBillCode());
			if(null == bill){
				throw new Exception("该单据不存在或已被撤销！");
			}
			if(bill.getBillState()!=2){
				throw new Exception("该单据已不符合签批条件！");
			}
			
			List<BalCashDrive> balList = balCashDriveService.findList(model.getBillCode(),2);
			double totalFee = 0.0;
			for (BalCashDrive cash : balList) {
				if(cash.getBalFee()!=null){
					totalFee = MathExtend.add(totalFee, cash.getBalFee());
				}
			}
			ActionContext.getContext().getValueStack().push(bill);
			ActionContext.getContext().put("balList", balList);
			ActionContext.getContext().put("totalFee", totalFee);
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		
		return "showCashMak";
	}
	
	/** 学校话费结算单据签批 */
	public String showTelfeeMak(){
		try {
			if(!ValidatorUtils.validateFixedLengthNumber(model.getBillCode(), 8, false)){
				throw new Exception("无法解析结算单据编号！");
			}
			//DatabaseContextHolder.setCustomerType("dataSourceTwo");
			
			BalMainBill bill = balMainBillService.getByCode(model.getBillCode());
			if(null == bill){
				throw new Exception("该单据不存在或已被撤销！");
			}
			if(bill.getBillState()!=2){
				throw new Exception("该单据已不符合签批条件！");
			}
			
			List<BalTelfeeDrive> balList = balTelfeeDriveService.findList(model.getBillCode(),2);
			double totalFee = 0.0;
			for (BalTelfeeDrive telfee : balList) {
				if(telfee.getBalFee()!=null){
					totalFee = MathExtend.add(totalFee, telfee.getBalFee());
				}
			}
			ActionContext.getContext().getValueStack().push(bill);
			ActionContext.getContext().put("balList", balList);
			ActionContext.getContext().put("totalFee", totalFee);
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("errorMsg", e.getMessage());
		}
		return "showTelfeeMak";
	}
	
	
	
	//************************************** 结 算 统 计   ***************************************************//
	
	/** 结算统计主页面 */
	public String statisticsMain(){
		ActionContext.getContext().put("currentDate", FormatDate.getDefaultDate());
		return "statisticsMain";
	}
	
	/** 结算统计明细页面 */
	public String statisticsList(){
		try {
			balMainBillService.statistics(model.getChkUserId(), model.getMakUserId(), model.getChkDate(), model.getMakDate());
		} catch (Exception e) {
			ActionContext.getContext().put("errorMsg", "页面加载错误！"+e.getMessage());
			e.printStackTrace();
		}
	
		return "statisticsList";
	}
	
}
